<template>
  <body id="poster">
  <el-form class="login-container" label-position="left"
           label-width="0px">
    <h3 class="login_title">初始化向导</h3>

    <el-form-item v-if="sign[0] === true">
      <h4 class="title">*修改用户名作为登录账号</h4>
      <el-input type="text" v-model="adminInit.adminId"
                auto-complete="off" placeholder="用户名"></el-input>
      <el-button type="primary" class="next_button" v-on:click='next_step(0)' >下一步</el-button>
    </el-form-item>
    <el-form-item v-if="sign[1]">
      <h4 class="title">*添加邮箱</h4>
      <el-input type="text" v-model="adminInit.email"
                auto-complete="off" placeholder="邮箱"></el-input>
      <el-button type="primary" class="next_button" v-on:click='next_step(1)' >下一步</el-button>
    </el-form-item>
    <el-form-item v-if="sign[2]">
      <h4 class="title">*修改密码</h4>
      <el-input type="password" v-model="adminInit.new_password"
                auto-complete="off" placeholder="密码"></el-input>
      <el-button type="primary" class="next_button" v-on:click='next_step(2)' >完成</el-button>
    </el-form-item>
  </el-form>
  </body>
</template>
<script>
  export default{
    name: 'appAdminInit',
    data() {
      return {
        sign :[true,false,false],
        adminInit: {
          adminId: '',
          email: '',
          new_password: ''
        },
        userToken: '',
      }
    },
    methods:{
      next_step(i){
        let that = this;
        if (i!==that.sign.length){
          if(i===0){
            this.$axios
              .get('/api/agent/isAgentOpenidExist', {
                params:{
                  agentOpenid: this.adminInit.adminId
                }
              }).then(success=>{
              that.sign[i]=false;
              that.sign[i+1]=true;
              this.$forceUpdate()
            }).catch(fail=>{})
          }
          if(i===1){
            this.$axios
              .get('/api/agent/isEmailExist', {
                params:{email: this.adminInit.email}
              }).then(success=>{
              that.sign[i]=false;
              that.sign[i+1]=true;
              this.$forceUpdate()
            }).catch(fail=>{})
          }
          if (i===2){
            console.log(this.adminInit)
            this.$axios
              .post('/api/agent/initAppAdmin', {
                adminId: this.adminInit.adminId,
                email: this.adminInit.email,
                new_password: this.adminInit.new_password,
                agentOpenid:this.$route.query.id
              })
              .then(success=>{
                this.$message({
                  type: 'info',
                  message: "初始化成功！"
                });
                localStorage.removeItem('Authorization')
                this.$router.replace('/login')
              }).catch(fail=>{this.$message.error("初始化失败")})
          }else {
            this.$forceUpdate()
          }
        }


      }
    }
  }
</script>
<style>
  #poster {
    background: url("../../assets/background.png") no-repeat;
    background-color: darkslateblue;
    background-position: center;
    height: 100%;
    width: 100%;
    position: fixed;
  }

  body {
    margin: 0px;
  }

  .login-container {

    position: center;
    border-radius: 15px;
    background-clip: padding-box;
    margin: 200px auto;
    width: 350px;
    padding: 35px 35px 15px 35px;
    background: transparent;
    border: 1px solid #eaeaea;
    box-shadow: 0 0 25px #cac6c6;
  }

  .login_title {
    margin: 0px auto 40px auto;
    text-align: center;
    color: #eaeaea;
  }
  .title{
    text-align: center;
    color: firebrick;
  }
  .next_button{
    width: 100%;
    background: #505458;
    border: none;
    margin-top: 10%;
  }

</style>
